package innersort.select;

import innersort.speed.SortStrategy;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-03 14:41
 * @ Description :
 */
public class SelectSort implements SortStrategy
{
    public static void main(String[] args)
    {
        int[] array = new int[8];
        for (int i = 0; i < 8; i++)
        {
            array[i] = (int)(Math.random() * 10);
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String beforeSort = format.format(date);
        System.out.println("beforeSort = " + beforeSort);

        selectSort(array);

        Date date1 = new Date();
        String afterSort = format.format(date1);
        System.out.println("afterSort = " + afterSort);
    }

    @Override
    public void sort(int[] array)
    {
        selectSort(array);
    }

    //升序
    public static void selectSort(int[] array)
    {
        //j: 表示需要进行多少次(array.length - 1)选择最小值的排序
        for (int j = 0; j < array.length - 1; j++)
        {
            //假设当前最小值的索引为0
            int minIndex = j;
            int min = array[j];

            //i: 表示每一次寻找最小值的过程
            for (int i = j + 1; i < array.length; i++)
            {
                if (array[i] < min)
                {
                    min = array[i];
                    minIndex = i;
                }
            }
            //交换(有需要时才交换)
            if (minIndex != j)
            {
                array[minIndex] = array[j];
                array[j] = min;
            }

            //System.out.println("第" + (j + 1) + "次排序后的序列为:" + Arrays.toString(array));
        }

    }

}
